<?php
namespace Api\Controller;
use Api\Library\ErrorCode;
use Api\Library\ErrorMsg;
use Api\Library\Response;
use Api\Library\Helper;
use Api\Helper\UserHelper;

class UserController extends ApiController
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 用户注册
     */
    public function registerPost()
    {
        UserHelper::registerPostValid();

        $reg_data = Helper::$param;
        $reg_data['money'] = 0;
        $reg_data['status'] = 1;
        $reg_data['user_type'] = 2;   //普通用户
        $reg_data['create_time'] = time();
        $reg_data['update_time'] = time();

        $userM = D('Api/User');
        $data = $userM->create($reg_data);

        if($data) {
            $res = $userM->add($data);
            if(!$res) {
                Response::getInstance()->setError(ErrorCode::FailedCode, ErrorMsg::FailedCode);
            }else {
                $resArr = array('uid' => $res);
                Response::getInstance()->setReply('code', ErrorCode::SuccessCode);
                Response::getInstance()->setReply('data', $resArr);
            }

            $this->ajaxReturn(Response::getInstance()->getReply());
        }
    }

    /**
     * 用户登入
     */
    public function loginPost()
    {
        UserHelper::loginPostValid();

        $map = array(
            'status' => 1,
        );
        $data = Helper::$param;
        $userM = D('Api/User');

        //用户名/手机号登入
        if(preg_match('/^1\d{10}$/', $data['username'])) {
            $map['mobile'] = array('eq', $data['username']);
        }else {
            $map['username'] = array('eq', $data['username']);
        }

        $res = $userM->where($map)->find();
        if(!$res) {
            Response::getInstance()->setError(ErrorCode::FailedCode, '用户不存在或被禁用！');
        }else {
            if($data['password'] !== $res['password']) {
                Response::getInstance()->setError(ErrorCode::FailedCode, '密码错误');
            }else {
                //记session
                $auth = array(
                    'uid'      => $res['id'],
                    'username' => $res['username']
                );
                $sessionName = 'user_auth::' . $res['id'];
                $signName = 'user_auth_sign::' . $res['id'];
                session('uid', $res['id']);
                session($sessionName, $auth);
                session($signName, $this->data_auth_sign($auth));
                Response::getInstance()->setReply('code', ErrorCode::SuccessCode);
                Response::getInstance()->setReply('data', $res);
            }
        }

        $this->ajaxReturn(Response::getInstance()->getReply());
    }

    /**
     * 用户注销
     */
    public function logoutPost()
    {
        if($this->is_login()) {
            $sessionName = 'user_auth::' . $this->getUid();
            $signName = 'user_auth_sign::' . $this->getUid();

            session($sessionName, null);
            session($signName, null);
            Response::getInstance()->setReply('code', ErrorCode::SuccessCode);
            Response::getInstance()->setReply('data', []);

            $this->ajaxReturn(Response::getInstance()->getReply());
        }
    }
}